Skip to content

Translate BigInteger division to div() - #4144

Open
Zentauer wants to merge 1 commit into
npgsql:mainfrom
Zentauer:bigint-division
Open

Zentauer wants to merge 1 commit into
npgsql:mainfrom
Zentauer:bigint-division

Conversation

@Zentauer

Copy link
Copy Markdown

Fixes #4143

BigInteger maps to numeric, so / between two BigInteger values was sent to PostgreSQL as numeric division. For 7 / 2 that returns 3.5, while .NET returns 3.

This translates / to div(a, b) when both operands are BigInteger (nullable included). div truncates towards zero, like BigInteger division in .NET. Division by zero fails in both cases. The change sits in NpgsqlSqlTranslatingExpressionVisitor.VisitBinary, next to the existing LocalDate subtraction case.

Before:

WHERE e."BigInteger" / 2 = 1

After:

WHERE div(e."BigInteger", 2) = 1

Tests added to BigIntegerTranslationsTest:

  • Divide: / 2 == 1 returns the rows with 2 and 3. Numeric division only returned 2.
  • Divide_truncates_towards_zero: / 2 == 0 returns the rows with 0, 1 and -1, so negative values are covered.
  • Divide_with_parameter: the divisor is a parameter.

BigInteger.Divide(a, b) still has no translation. Should I add it to this PR as well, or keep that separate?

BigInteger maps to numeric, so `/` between two BigInteger values ran as
numeric division in PostgreSQL and returned 3.5 for 7 / 2. .NET truncates
towards zero and returns 3. Translate the division to div(a, b), which
truncates the same way.

Fixes npgsql#4143

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BigInteger division returns fractional results instead of integer division

1 participant